﻿using CPPEI.BD.VideoMatrixConfigTool.Datas;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CPPEI.BD.VideoMatrixConfigTool
{

    public class XSolutionConfigUntil
    {
        private static List<Solution> ListSolution;

        private static List<Item> ListVideoSource;

        /// <summary>
        ///  根据方案名称获取对应的视频源和视频矩阵的对应关系
        /// </summary>
        /// <param name="name">方案名称</param>
        /// <param name="configFile">配置文件的路径</param>
        /// <returns></returns>
        public static List<Solution> GetListSolutionByName(string name, string configFile)
        {
            if (System.IO.File.Exists(configFile))
            {
                XmlDocument xmlDocument = new XmlDocument();
                try
                {
                    xmlDocument.Load(configFile);

                }
                catch (Exception ex)
                {
                    throw ex;
                }

                string rootName = "Solution-List";

                XmlNode rootNode = null;
                ListSolution = new List<Solution>();
                try
                {
                    ////根据xClassConfig的Name获取根节点
                    rootNode = xmlDocument.SelectSingleNode("./" + rootName);

                    if (rootNode == null || rootNode.ChildNodes == null)
                    {
                        return null;
                    }

                    foreach (XmlNode childNode in rootNode.ChildNodes)
                    {
                        XmlElement xe = (XmlElement)childNode;
                        if (name.Equals(xe.GetAttribute("Name").ToString()))
                        {
                            Solution solution = new Solution();
                            solution.Name = xe.GetAttribute("Name").ToString();
                            solution.Caption = xe.GetAttribute("Caption").ToString();
                            // 得到solution节点的所有子节点
                            XmlNodeList childNodeList = xe.ChildNodes;
                            solution.Mapping = new List<Mapping>();

                            foreach (XmlNode node in childNodeList)
                            {
                                Mapping mapping = new Mapping();
                                XmlElement element = (XmlElement)node;
                                mapping.VideoMatrixId = element.GetAttribute("VideoMatrixId");
                                mapping.VideoSourceId = element.GetAttribute("VideoSourceId");
                                solution.Mapping.Add(mapping);
                            }
                        }

                    }

                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
            return ListSolution;
        }

        /// <summary>
        ///  根据方案名称获取对应的视频源和视频矩阵的对应关系
        /// </summary>
        /// <param name="name">方案名称</param>
        /// <param name="configFile">配置文件的路径</param>
        /// <returns></returns>
        public static List<Solution> GetListSolution(string configFile)
        {
            if (System.IO.File.Exists(configFile))
            {
                XmlDocument xmlDoc = new XmlDocument();
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments = true;
                XmlReader reader = XmlReader.Create(configFile, settings);
                xmlDoc.Load(reader);

                string rootName = "Solution-List";

                XmlNode rootNode = null;
                ListSolution = new List<Solution>();
                try
                {
                    ////根据xClassConfig的Name获取根节点
                    rootNode = xmlDoc.SelectSingleNode("./" + rootName);

                    if (rootNode == null || rootNode.ChildNodes == null)
                    {
                        return null;
                    }

                    foreach (XmlNode childNode in rootNode.ChildNodes)
                    {
                        XmlElement xe = (XmlElement)childNode;

                        // 得到solution节点的所有子节点
                        XmlNodeList childNodeList = xe.ChildNodes;
                        // solution.Mapping = new List<Mapping>();

                        foreach (XmlNode node in childNodeList)
                        {
                            Solution solution = new Solution();
                            solution.Name = xe.GetAttribute("Name").ToString();
                            solution.Caption = xe.GetAttribute("Caption").ToString();

                            XmlElement element = (XmlElement)node;
                            solution.VideoSourceId = element.GetAttribute("VideoSourceId");
                            solution.VideoMatrixId = element.GetAttribute("VideoMatrixId");

                            ListSolution.Add(solution);
                        }

                    }

                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    reader.Close();
                }

            }
            return ListSolution;
        }


        /// <summary>
        /// 获取视频源信息或者获取矩阵源信息
        /// </summary>
        /// <param name="configFile"></param>
        /// <returns></returns>
        public static List<Item> GetListVideoOrMatrixSource(string configFile, string rootName)
        {
            if (System.IO.File.Exists(configFile))
            {
                XmlDocument xmlDoc = new XmlDocument();
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments = true;
                XmlReader reader = XmlReader.Create(configFile, settings);
                xmlDoc.Load(reader);

                // rootName = "VideoSource";

                XmlNode rootNode = null;
                ListVideoSource = new List<Item>();
                try
                {
                    ////根据xClassConfig的Name获取根节点
                    rootNode = xmlDoc.SelectSingleNode("./" + rootName);

                    if (rootNode == null || rootNode.ChildNodes == null)
                    {
                        return null;
                    }

                    foreach (XmlNode childNode in rootNode.ChildNodes)
                    {
                        XmlElement xe = (XmlElement)childNode;


                        Item item = new Item();
                        item.Id = xe.GetAttribute("Id").ToString();
                        item.Caption = xe.GetAttribute("Caption").ToString();
                        ListVideoSource.Add(item);

                    }

                }
                catch (Exception ex)
                {
                    throw ex;
                }


            }
            return ListVideoSource;
        }

        /// <summary>
        /// 修改Solution信息
        /// </summary>
        /// <param name="solutionFilePath"></param>
        /// <param name="solution"></param>
        /// <returns></returns>
        public static bool UpdateSolution(string solutionFilePath, Datas.Solution oSolution, Datas.Solution cSolution)
        {
            if (System.IO.File.Exists(solutionFilePath) && oSolution != null && cSolution != null)
            {
                XmlDocument xmlDoc = new XmlDocument();
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments = true;
                XmlReader reader = XmlReader.Create(solutionFilePath, settings);
                xmlDoc.Load(reader);

                string rootName = "Solution-List";
                XmlNode rootNode = null;
                try
                {
                    rootNode = xmlDoc.SelectSingleNode("./" + rootName);

                    if (rootNode == null || rootNode.ChildNodes == null)
                    {
                        return false;
                    }
                    foreach (XmlNode childNode in rootNode.ChildNodes)
                    {
                        XmlElement xe = (XmlElement)childNode;
                        if (oSolution.Name == xe.GetAttribute("Name").ToString() && oSolution.Caption == xe.GetAttribute("Caption").ToString())
                        {
                            xe.SetAttribute("Name", cSolution.Name);
                            xe.SetAttribute("Caption", cSolution.Caption);
                            if (childNode.ChildNodes != null)
                            {
                                foreach (XmlNode node in childNode.ChildNodes)
                                {
                                    XmlElement xeNode = (XmlElement)node;
                                    if (oSolution.VideoSourceId == xeNode.GetAttribute("VideoSourceId").ToString() && oSolution.VideoMatrixId == xeNode.GetAttribute("VideoMatrixId").ToString())
                                    {
                                        xeNode.SetAttribute("VideoSourceId", cSolution.VideoSourceId);
                                        xeNode.SetAttribute("VideoMatrixId", cSolution.VideoMatrixId);
                                        break;
                                    }
                                }
                            }
                            break;
                        }

                    }
                    reader.Close();
                    xmlDoc.Save(solutionFilePath);
                    return true;

                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
            return false;
        }

        /// <summary>
        /// 添加Solution信息
        /// </summary>
        /// <param name="solutionFilePath"></param>
        /// <param name="oSolution"></param>
        /// <param name="cSolution"></param>
        /// <returns></returns>
        public static bool AddSolution(string solutionFilePath, Datas.Solution cSolution)
        {
            if (System.IO.File.Exists(solutionFilePath) && cSolution != null)
            {
                XmlDocument xmlDoc = new XmlDocument();
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments = true;
                XmlReader reader = XmlReader.Create(solutionFilePath, settings);
                xmlDoc.Load(reader);

                string rootName = "Solution-List";
                XmlNode rootNode = null;
                try
                {
                    rootNode = xmlDoc.SelectSingleNode("./" + rootName);

                    if (rootNode == null || rootNode.ChildNodes == null)
                    {
                        return false;
                    }
                    foreach (XmlNode childNode in rootNode.ChildNodes)
                    {
                        XmlElement xe = (XmlElement)childNode;
                        //将信息添加到现有方案中
                        if (cSolution.Name == xe.GetAttribute("Name").ToString() && cSolution.Caption == xe.GetAttribute("Caption").ToString())
                        {
                            // xe.SetAttribute("Name", cSolution.Name);
                            // xe.SetAttribute("Caption", cSolution.Caption);
                            if (childNode.ChildNodes != null)
                            {
                                foreach (XmlNode node in childNode.ChildNodes)
                                {
                                    XmlElement xeNode = (XmlElement)node;
                                    if (cSolution.VideoSourceId != xeNode.GetAttribute("VideoSourceId").ToString() || cSolution.VideoMatrixId != xeNode.GetAttribute("VideoMatrixId").ToString())
                                    {
                                        XmlElement mappingXe = xmlDoc.CreateElement("Mapping");
                                        mappingXe.SetAttribute("VideoSourceId", cSolution.VideoSourceId);
                                        mappingXe.SetAttribute("VideoMatrixId", cSolution.VideoMatrixId);


                                        childNode.AppendChild(mappingXe);

                                        reader.Close();
                                        xmlDoc.Save(solutionFilePath);
                                        return true;
                                    }
                                }
                            }

                            break;
                        }

                    }
                    //创建新的方案，将配置信息写入方案中
                    XmlElement createXe = xmlDoc.CreateElement("Solution");
                    createXe.SetAttribute("Name", cSolution.Name);
                    createXe.SetAttribute("Caption", cSolution.Caption);

                    XmlElement mapXe = xmlDoc.CreateElement("Mapping");
                    mapXe.SetAttribute("VideoSourceId", cSolution.VideoSourceId);
                    mapXe.SetAttribute("VideoMatrixId", cSolution.VideoMatrixId);
                    createXe.AppendChild(mapXe);

                    rootNode.AppendChild(createXe);


                    reader.Close();
                    xmlDoc.Save(solutionFilePath);
                    return true;

                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
            return false;
        }

        /// <summary>
        /// 删除Solution信息
        /// </summary>
        /// <param name="solutionFilePath"></param>
        /// <param name="oSolution"></param>
        /// <param name="cSolution"></param>
        /// <returns></returns>
        public static bool DeleteSolution(string solutionFilePath, Datas.Solution cSolution)
        {
            if (System.IO.File.Exists(solutionFilePath) && cSolution != null)
            {
                XmlDocument xmlDoc = new XmlDocument();
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments = true;
                XmlReader reader = XmlReader.Create(solutionFilePath, settings);
                xmlDoc.Load(reader);

                string rootName = "Solution-List";
                XmlNode rootNode = null;
                try
                {
                    rootNode = xmlDoc.SelectSingleNode("./" + rootName);

                    if (rootNode == null || rootNode.ChildNodes == null)
                    {
                        return false;
                    }
                    foreach (XmlNode childNode in rootNode.ChildNodes)
                    {
                        XmlElement xe = (XmlElement)childNode;
                     
                        if (cSolution.Name == xe.GetAttribute("Name").ToString() && cSolution.Caption == xe.GetAttribute("Caption").ToString())
                        {
                            // xe.SetAttribute("Name", cSolution.Name);
                            // xe.SetAttribute("Caption", cSolution.Caption);
                            if (childNode.ChildNodes != null)
                            {
                                foreach (XmlNode node in childNode.ChildNodes)
                                {
                                    XmlElement xeNode = (XmlElement)node;
                                    if (cSolution.VideoSourceId == xeNode.GetAttribute("VideoSourceId").ToString() && cSolution.VideoMatrixId == xeNode.GetAttribute("VideoMatrixId").ToString())
                                    {
                                        if (childNode.ChildNodes.Count == 1)
                                        {
                                            rootNode.RemoveChild(childNode);
                                        }
                                        xe.RemoveChild(node);
                                    }
                                }
                            }

                            break;
                        }

                    }

                    reader.Close();
                    xmlDoc.Save(solutionFilePath);
                    return true;

                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
            return false;
        }
    }
}
